<?php
class ActiveRecordLogBehavior extends CActiveRecordBehavior
{
    private $_oldattributes = array();
 
    public function afterSave($event)
    {
        // Get current user
        $user = User::model()->findByPK(Yii::app()->user->id);
    
        // Do we have an already existing or a new record?
        if (!$this->Owner->isNewRecord) { // {{{ Existing record
 
            // new attributes
            $newattributes = $this->Owner->getAttributes();
            $oldattributes = $this->getOldAttributes();

            //echo par($oldattributes);
            //echo par($newattributes);

            // compare old and new
            foreach ($newattributes as $name => $newValue) {
                //echo "<li>$name: $newValue";
                if (!empty($oldattributes)) {
                    $oldValue = $oldattributes[$name];
                } else {
                    $oldValue = '';
                }
 
                if ($newValue !== $oldValue and (trim($oldValue)!=='' and trim($newValue)!=='')) {
                    //echo "<br/>&rArr; Was: $oldValue";
                    $changes = "Old Value:\n$oldValue\nNew Value:\n$newValue";
                    $log=new ActiveRecordLog;
                    $log->modelName     = get_class($this->Owner);
                    $log->fieldName     = $name;
                    $log->action        = ActiveRecordLog::ACTION_CHANGE;
                    $log->recordId      = $this->Owner->getPrimaryKey();
                    $log->userId        = Yii::app()->user->id;
                    $log->timestamp     = time();
                    $log->description   =  $changes;
                    $log->save();
                }
            }
            // }}} 
        } else { // {{{ New record
            $log=new ActiveRecordLog;
            $log->description=  'New record created';
            $log->modelName     = get_class($this->Owner);
            $log->fieldName     = 'n/a';
            $log->action        = ActiveRecordLog::ACTION_CREATE;
            $log->recordId      = $this->Owner->getPrimaryKey();
            $log->userId        = Yii::app()->user->id;
            $log->timestamp     = time();
            $log->save();
            // }}} 
        }
    }
 
    public function afterDelete($event)
    {
        // Get current user
        $user = User::model()->findByPK(Yii::app()->user->id);
    
        $log=new ActiveRecordLog;
        $log->description=  sprintf(
            'User %s deleted record %s[%s]',
            $user->getName(User::NAMETYPE_FIRST1_LAST),
            get_class($this->Owner), $this->Owner->getPrimaryKey()
        );
        $log->modelName = get_class($this->Owner);
        $log->fieldName = '';
        $log->action    = ActiveRecordLog::ACTION_DELETE;
        $log->recordId  = $this->Owner->getPrimaryKey();
        $log->userId    = Yii::app()->user->id;
        $log->timestamp = time();
        $log->save();
    }
 
    public function afterFind($event)
    {
        // Save old values
        $this->setOldAttributes($this->Owner->getAttributes());
    }
 
    public function getOldAttributes()
    {
        return $this->_oldattributes;
    }
 
    public function setOldAttributes($values)
    {
        $this->_oldattributes=$values;
    }
}

/* vim:set ai sw=4 sts=4 et fdm=marker fdc=4: */ 
?>
